import tushare as ts
import pandas as pd 
import sys
from textrank4zh import TextRank4Sentence
import utils
ts.set_token('f3de3fc9f53ab45aae16550524a17052427a6d30351c3881652ed044')


def getAbstract(sentence):
    tr4s = TextRank4Sentence()
    tr4s.analyze(text=sentence, lower=True, source='all_filters')

    abstract = []
    for item in tr4s.get_key_sentences(num=100):
        if len(item.sentence) < 300:
            abstract.append([item.index,item.sentence])

    abstract = sorted(abstract[:2], key=lambda x: x[0])
    abstract = ["%s。" % (x[1]) for i, x in enumerate(abstract, 1)]
    return "".join(abstract)

def getNews(formated_day):
	pro = ts.pro_api()
	#formated_day = utils.getYesterday().strftime('%Y%m%d')
	news = pro.cctv_news(date=formated_day)
	#df.to_csv('news_{}.csv'.format(formated_today),encoding='utf-8-sig')
	contents = "".join(list(news['content']))
	abstracts = ["%s" % (getAbstract(x)) for x in news['content']]
	news['abstract'] = abstracts
	news.to_csv('news/%s.csv' % (formated_day),encoding='utf-8-sig')
